package com.rzico.picture;

import com.alibaba.fastjson.JSONObject;
import com.rzico.core.entity.SysUser;
import com.rzico.exception.CustomException;
import com.rzico.picture.entity.Picture;
import com.rzico.picture.entity.PointAddress;
import com.rzico.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.*;
import org.opencv.core.Point;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.core.parameters.P;
import sun.awt.image.BufferedImageGraphicsConfig;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;


/**
 * @author tqf
 * @version 创建时间：2020-4-3 上午10:49:02
 * 类说明:图片添加水印   文字&图片水印
 */
@Slf4j
public class PictureUtils {

    // 水印透明度
    private double alpha = 0.5f;
    // 水印横向位置
    private int positionWidth = 100;
    // 水印纵向位置
    private int positionHeight = 100;
    // 水印文字字体
    private static Integer fontSize = 35;
    // 字体方向
    private static Integer dv = 0;
    // 字体方向
    private static Integer rw = 0;
    // 水印文字颜色
    private static Color color = Color.BLACK;

    private static String fontFileName = "SaaSeriesBD.ttf";

    public static Font loadFont(String fontFileName, float fontSize)  //第一个参数是外部字体名，第二个是字体大小
    {
        try
        {
            ClassPathResource resource = new ClassPathResource(fontFileName);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, resource.getInputStream());
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
            return dynamicFontPt;
        }
        catch(Exception e)//异常处理
        {
            e.printStackTrace();

            return new java.awt.Font("宋体", Font.BOLD,new BigDecimal(fontSize).intValue());
        }
    }

    /**
     *
     * @param alpha
     *            水印透明度
     * @param positionWidth
     *            水印横向位置
     * @param positionHeight
     *            水印纵向位置
     * @param fontSize
     *            水印文字字体
     * @param color1
     *            水印文字颜色
     */
    public void setImageMarkOptions(double alpha, int positionWidth,
                                           int positionHeight, Integer fontSize, Color color1,Integer dv,Integer rw) {
        this.alpha = alpha;
        this.positionWidth = positionWidth;
        this.positionHeight = positionHeight;
        this.fontSize = fontSize;
        if (color1!=null) {
            color = color1;
        }
        this.dv = dv;
        this.rw = rw;
    }



    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     *
     * @param logoText
     * @param degree
     */
    public BufferedImage markImageByText(String logoText,Integer degree) {

        InputStream is = null;
        OutputStream os = null;
        try {

            Font font = loadFont(fontFileName, fontSize); //new Font("宋体", Font.BOLD, fontSize);
            FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);
            if (1 == dv) {
                int singleWidth = fm.charWidth('9');  //8
                int height = fm.getHeight();        //21
                BufferedImage buffImg = new BufferedImage(singleWidth,height*logoText.length(), BufferedImage.TYPE_INT_RGB);

                BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(buffImg);
                buffImg =config.createCompatibleImage(singleWidth,height*logoText.length(), Transparency.TRANSLUCENT);
                Graphics2D g = buffImg.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                // 5、设置水印文字颜色
                g.setColor(color);
                // 6、设置水印文字Font
                g.setFont(font);
                // 7、设置水印文字透明度
                if (alpha!=0.0) {
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) alpha));
                }
                //竖排文字
                int strlength = logoText.length();
                //获取字体高度
                int t = height;
                for(int i=0;i<strlength;i++){
                    g.drawString(String.valueOf(logoText.charAt(i)), 0,t*i+t);

                }
                // 9、释放资源
                g.dispose();
                return buffImg;
            } else {
                //获取字体宽度
                //横排文字
                int strlength = logoText.length();
                //获取字体宽度
                int t = fm.getHeight();

                Integer width = fm.charWidth('9')*strlength + 3;
                Integer height = fm.getHeight();

                //在哪个字符换行
                if (rw > 0) {
                    width = fm.charWidth('9')*rw +3;
                    //默认最多2行
                    height = fm.getHeight()*2;
                }
                BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

                BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(buffImg);
                buffImg =config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

                Graphics2D g = buffImg.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                // 5、设置水印文字颜色
                g.setColor(color);
                // 6、设置水印文字Font
                g.setFont(font);
                // 7、设置水印文字透明度
                if (alpha!=0.0) {
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) alpha));
                }
                if (rw > 0){
                    String s1 = logoText.substring(0,rw);
                    String s2 = logoText.substring(rw);
                    //y=25,上方有边距，需要设置成20才可显示
                    g.drawString(s1, 0, 20);
                    int y = t + 18;
                    g.drawString(s2, 0, y);
//                    OutputStream os1 = new FileOutputStream("D:/Ideal2017Space/rzico-picture/1x.png");
//                    ImageIO.write(buffImg, "PNG", os1);
                }else {
                    g.drawString(logoText, 0, 20);
//                    OutputStream os1 = new FileOutputStream("D:/Ideal2017Space/rzico-picture/1x.png");
//                    ImageIO.write(buffImg, "PNG", os1);
                }

                // 9、释放资源
                g.dispose();
                return buffImg;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对图片进行旋转
     *
     * @param src  被旋转图片
     * @param angel 旋转角度
     * @return 旋转后的图片
     */
    /**
     *
     * @param bufferedImage
     *            图片
     * @param angel
     *            旋转角度
     * @return
     */
    public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {
        if (bufferedImage == null) {
            return null;
        }
        try {
/*            OutputStream os = null;
            os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/1-3.png");
            ImageIO.write(bufferedImage, "PNG", os);*/

            if (angel < 0) {
                // 将负数角度，纠正为正数角度
                angel = angel + 360;
            }
            int imageWidth = bufferedImage.getWidth(null);
            int imageHeight = bufferedImage.getHeight(null);
            // 计算重新绘制图片的尺寸
            Rectangle rectangle = CalcRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);
            // 获取原始图片的透明度
            int type = bufferedImage.getColorModel().getTransparency();
            BufferedImage newImage = null;
            newImage = new BufferedImage(rectangle.width, rectangle.height, type);
            Graphics2D graphics = newImage.createGraphics();
            // 平移位置
            graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
            // 旋转角度
            graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
            // 绘图
            graphics.drawImage(bufferedImage, null, null);
//            os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/xz.png");
//            ImageIO.write(newImage, "PNG", os);
            return newImage;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 计算旋转后的图片
     *
     * @param src  被旋转的图片
     * @param angel 旋转角度
     * @return 旋转后的图片
     */
    public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
        // 如果旋转的角度大于90度做相应的转换
        if (angel >= 90) {
            if (angel / 90 % 2 == 1) {
                int temp = src.height;
                src.height = src.width;
                src.width = temp;
            }
            angel = angel % 90;
        }

        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angel_dalta_width = Math.atan((double) src.height / src.width);
        double angel_dalta_height = Math.atan((double) src.width / src.height);

        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_width));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_height));
        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;
        return new Rectangle(new Dimension(des_width, des_height));
    }

    /**
     * 给图片添加图片水印、可设置水印文字的旋转角度
     *
     * @param logoText
     * @param degree
     */
    public BufferedImage markImageByImageFont(String logoText,Image srcImg,
                               Integer degree) {

        InputStream is = null;
        OutputStream os = null;
        try {
            // 1、源图片
            //BufferedImage srcImg = ImageIO.read(new File(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);

            BufferedImage orgImg = convertImageFont(logoText);

            //旋转图片
            BufferedImage waterImg = rotateImage(orgImg,degree);


            int  width = positionWidth;
            int  heigth = positionHeight;

            g.drawImage(waterImg.getScaledInstance(waterImg.getWidth(null),waterImg.getHeight(null), Image.SCALE_SMOOTH), width, heigth,null);

            // 9、释放资源
            g.dispose();

            return buffImg;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static BufferedImage convertImageFont(String text) {

        BufferedImage buffImg = new BufferedImage(575,348*text.length(), BufferedImage.TYPE_INT_RGB);

        BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(buffImg);
        buffImg =config.createCompatibleImage(575,348*text.length(), Transparency.TRANSLUCENT);

        // 2、得到画笔对象
        Graphics2D g = buffImg.createGraphics();

        try {

            int i=text.length()-1;
            int z=0;
            while (i>=0) {
                String w = text.substring(i,i+1);
                ClassPathResource resource = new ClassPathResource("images/"+w+".png");
                if (resource.exists()) {
                    Image srcImg = ImageIO.read(resource.getInputStream());
                    g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 348*z, null);
                    System.out.println(w);
                }
                i--;
                z++;

            }
            g.dispose();


            // targetW，targetH分别表示目标长和宽
            Integer width = fontSize;
            BufferedImage target = null;

            double sx = (double) width / buffImg.getWidth();
            double sy = 0;

            // 等比缩放
            sy = sx;
            Integer height = (int) (sy * buffImg.getHeight());

            ColorModel cm = buffImg.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();

            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
            Graphics2D g2 = target.createGraphics();
            // smoother than exlax:
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2.drawRenderedImage(buffImg, AffineTransform.getScaleInstance(sx, sy));
            g2.dispose();

            return target;

            // 10、生成图片
        } catch (Exception e) {

        }

        return null;

    }

    public BufferedImage createImage(Picture picture,Integer isDrat,BufferedImage srcImg,String no1,String no2,String no3) {

        Color color = null;
        if (picture.getFontColor()!=null) {
            color = new Color(Integer.parseInt(picture.getFontColor().substring(1), 16));
        }
        setImageMarkOptions(picture.getAlpha(),picture.getPx(),picture.getPy(),picture.getFontSize(),color,picture.getDv(),picture.getRw());
        if (picture.getType().equals(0)) {
//            return markImageByText(no1,srcImg,picture.getAngle());
            if (isDrat==1){
                return PictureUtils.makeRateImage(no1, srcImg, picture);
            }else {
                return PictureUtils.makeImageTxt(no1, srcImg, picture.getPx(), picture.getPy(), picture.getFontSize(), picture.getAlpha(), picture.getAngle(), picture.getDv(), picture.getRw());
            }

        } else if (picture.getType().equals(1)) {
            return markImageByImageFont(no2,srcImg,picture.getAngle());
        } /*else {
            return markImageByText(no3,srcImg,picture.getAngle());

        }*/
        return null;
    }


    public Mat warpPerspective(Mat src, Picture picture) {

     /*   Point p1 = new Point(0,0);
        Point p2 = new Point(8,0);
        Point p3 = new Point(8,231);
        Point p4 = new Point(0,231);*/
        Point p1 = new Point(0,0);
        Point p2 = new Point(8,0);
        Point p3 = new Point(0,331); //231 + 23.1
        Point p4 = new Point(8,331);
        // 点的顺序[左上 ，右上 ，右下 ，左下]
        List<Point> listSrcs = java.util.Arrays.asList(p1,p2,p3,p4);


        Mat srcPoints = Converters.vector_Point_to_Mat(listSrcs, CvType.CV_32F);
        if (StringUtils.isEmpty(picture.getAttch())){
            throw new CustomException("没有旋转参数");
        }
        PointAddress pointAddress = JSONObject.parseObject(picture.getAttch(), PointAddress.class);

        Point d1 = new Point(pointAddress.getX1(),pointAddress.getY1());  //0,50    0, 0
        Point d2 = new Point(pointAddress.getX2(),pointAddress.getY2());  //80,0    8, 5
        Point d3 = new Point(pointAddress.getX3(),pointAddress.getY3());  //0, 579  0, 231
        Point d4 = new Point(pointAddress.getX4(),pointAddress.getY4()); //80,629   8, 220

/*        Point d1 = new Point(0, 0);  //0,50    0, 0
        Point d2 = new Point(8, 5);  //80,0    8, 5
        Point d3 = new Point(0 ,254.1);  //0, 579  0, 231
        Point d4 = new Point(8, 249.7); //80,629   8, 220*/

        // 点的顺序[左上 ，右上 ，右下 ，左下]
        List<Point> listDsts = java.util.Arrays.asList(d1,d2,d3,d4);

        Mat dstPoints = Converters.vector_Point_to_Mat(listDsts, CvType.CV_32F);

        Mat perspectiveMmat = Imgproc.getPerspectiveTransform(srcPoints, dstPoints);

        Mat dst = new Mat();

        Imgproc.warpPerspective(src, dst, perspectiveMmat, src.size(), Imgproc.INTER_LINEAR + Imgproc.WARP_INVERSE_MAP,
                1, new Scalar(0));

        return dst;

    }

/*    public static void main(String[] args) {

        try {
        // 原始图片路径
        String srcImgPath = "D:/Ideal2017Space/rzico-picture/1.jpg";  //只需要这一张图片 然后生成4张图片
        // 添加的文本
        String logoText = "AMY1234567890";

//        String targerTextPath = "D:/Ideal2017Space/rzico-picture/1-1.jpg";  //添加文字水印之后的图片存放路径

        System.out.println("给图片添加水印文字开始...");

        PictureUtils pictureUtils = new PictureUtils();
        Image srcImg = ImageIO.read(new File(srcImgPath));



         // 给图片添加水印文字,水印文字旋转-45
        pictureUtils.setImageMarkOptions(0,0,0,25,Color.red,0,0);
            BufferedImage buf = pictureUtils.markImageByText(logoText, 0);

                 OutputStream os = null;
            os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/1-5.png");
        ImageIO.write(buf, "PNG", os);





            System.load("D:/Ideal2017Space/rzico-picture/OpenCV_4_1_1/build/java/x64/opencv_java411.dll");
            Mat srcMat = ImgUtils.matFactory("D:/Ideal2017Space/rzico-picture/1-3.png");
            Mat dstMat = pictureUtils.warpPerspective(srcMat);
            ImgUtils.saveImg(dstMat,"D:/Ideal2017Space/rzico-picture/1-6.png");


            BufferedImage src1Img = ImageIO.read(new File("D:/Ideal2017Space/rzico-picture/1.jpg"));
            BufferedImage destImg = ImageIO.read(new File("D:/Ideal2017Space/rzico-picture/1-6.png"));

            BufferedImage buffImg = new BufferedImage(src1Img.getWidth(null),src1Img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(src1Img.getScaledInstance(src1Img.getWidth(null),src1Img.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
            g.drawImage(destImg.getScaledInstance(destImg.getWidth(null),destImg.getHeight(null), Image.SCALE_SMOOTH), 100, 100,null);
            g.dispose();

            OutputStream os2 = new FileOutputStream("D:/Ideal2017Space/rzico-picture/dest.png");
            ImageIO.write(buffImg, "PNG", os2);

        } catch (Exception e) {

        }


    }*/



    public static void main(String[] args) {
        try {
//            makeImageTxt("123456", "", 20, 0, 0);
            PointAddress point = new PointAddress();
            point.setX1(0);
            point.setY1(0);
            point.setX2(8);
            point.setY2(5);
            point.setX3(0);
            point.setY3(231);
            point.setX4(8);
            point.setY4(220);
            System.out.println(JSONObject.toJSONString(point));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 四角转换处理图片
     * 柜号
     */
    public static BufferedImage makeRateImage(String logoText, BufferedImage srcImg, Picture picture){
        Date date = new Date();
        String strDateFormat = "yyyyMMdd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String rateFileName = String.valueOf(date.getTime()) + ".png";
        String dllPath = "C://dll//x64//opencv_java411.dll";
//        String dllPath = "opencv_java411.dll";
        String matFile = "mat" + String.valueOf(date.getTime()) + ".png";
        String destPath = "C:/images/src/"+sdf.format(date);
        File filePath = new File(destPath);
        filePath.mkdirs();

        try {
            Font font = loadFont(fontFileName, picture.getFontSize());
            PictureUtils pictureUtils = new PictureUtils();
//            Color color = new Color(66,70,69);
            pictureUtils.setImageMarkOptions(0,0,50, picture.getFontSize(), color, picture.getDv(), picture.getRw());
            BufferedImage destBuffer = pictureUtils.markImageByText(logoText, 0);
            OutputStream os1 = null;
            os1 = new FileOutputStream(destPath+"/"+rateFileName);
            ImageIO.write(destBuffer, "PNG", os1);

            System.load(dllPath);
            Mat srcMat = ImgUtils.matFactory(destPath+"/"+rateFileName);
            Mat dstMat = pictureUtils.warpPerspective(srcMat, picture);

            ImgUtils.saveImg(dstMat,matFile);

            BufferedImage destImg = ImageIO.read(new File(matFile));

            //旋转图片
            if (picture.getAngle() != 0){
                destImg = rotateImage(destImg, picture.getAngle());
            }

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            g.setFont(font);
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
//            g.drawImage(destImg.getScaledInstance(destImg.getWidth(null),destImg.getHeight(null), Image.SCALE_SMOOTH), 314, 168,null);
            g.drawImage(destImg.getScaledInstance(destImg.getWidth(null),destImg.getHeight(null), Image.SCALE_SMOOTH), 314, 168,9,203,null);

            // targetW，targetH分别表示目标长和宽
          /*  Integer width = 9;
            BufferedImage target = null;

            double sx = (double) width / destImg.getWidth();
            double sy = 0;

            // 等比缩放
            sy = sx;
            Integer height = (int) (sy * destImg.getHeight());

            ColorModel cm = destImg.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();

            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
            // smoother than exlax:
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g.drawRenderedImage(target, AffineTransform.getScaleInstance(sx, sy));
            g.drawImage(target.getScaledInstance(target.getWidth(null),target.getHeight(null), Image.SCALE_SMOOTH), 314, 168,null);
*/
            g.dispose();
            return buffImg;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 正面图片生成文字
     * 箱号
     */
    public static BufferedImage makeImageTxt(String logoText, BufferedImage srcImg, Integer px, Integer py, Integer fontSize, Double alpha, Integer angle, Integer dv, Integer rw){
        try {
            System.out.println("给图片添加水印文字开始...");
            Font font = loadFont(fontFileName, fontSize);

            PictureUtils pictureUtils = new PictureUtils();
            Graphics2D g = srcImg.createGraphics();
//            Color color = new Color(182,196,207);
            pictureUtils.setImageMarkOptions(alpha,0,0,fontSize, color, dv, rw);
            BufferedImage destBuffer = pictureUtils.markImageByText(logoText, 0);
            g.setFont(font);
            //旋转
            if (angle != 0){
                destBuffer = rotateImage(destBuffer, angle);
            }

            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
            //字体输出位置
            g.drawImage(destBuffer.getScaledInstance(destBuffer.getWidth(null),destBuffer.getHeight(null), Image.SCALE_SMOOTH), px, py,null);
            g.dispose();

/*            OutputStream os = null;
            os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/txtImage.png");
            ImageIO.write(srcImg, "PNG", os);*/
            return srcImg;
        } catch (Exception e){
            e.printStackTrace();
        }
        return srcImg;
    }


    public static void drawLine() throws Exception{
        int width = 200, height = 250;

        BufferedImage buffImg = new BufferedImage(width ,height, BufferedImage.TYPE_INT_RGB);
        //设置背景透明
        BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(buffImg);
        buffImg =config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

        Graphics2D g2d = buffImg.createGraphics();//将对象g类型从Graphics转换成Graphics2D
        //起点是(30，30)，终点是(340，30)
        Line2D line = new Line2D.Double(30.0,30.0,340.0,30.0);
        g2d.draw(line);
//        RoundRectangle2D rRect = new RoundRectangle2D.Double(13.0,30.0,100.0,70.0,40.0,20.0);
//        g2d.draw(rRect);

        OutputStream os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/line.png");
        ImageIO.write(buffImg, "PNG", os);
    }


    public static void drawEllipse() throws Exception{
        int width = 200, height = 250;
        //创建图片对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        //基于图片对象打开绘图
        Graphics2D graphics = image.createGraphics();
        //绘图逻辑 START （基于业务逻辑进行绘图处理）……

        //绘制圆形
        graphics.setColor(Color.BLACK);
        Ellipse2D.Double ellipse = new Ellipse2D.Double(120, 120, 100, 100);
        graphics.draw(ellipse);
        // 绘图逻辑 END
        //处理绘图
        graphics.dispose();
        //将绘制好的图片写入到图片
        ImageIO.write(image, "png", new File("ellipse.png"));

//        OutputStream os = new FileOutputStream("D:/Ideal2017Space/rzico-picture/ellipse.png");
//        ImageIO.write(image, "PNG", os);
    }


}
